<script  lang="ts">
import { defineComponent } from 'vue';
import AiChatWindow from "@/views/AiChatWindow.vue";

/**
 * 主应用组件
 * 负责管理AI聊天窗口的显示与隐藏，以及全局样式配置
 */
export default defineComponent({
  components: {
    AiChatWindow
  },
  data() {
    return {
      /**
       * 控制AI聊天窗口是否显示的状态
       * @type {boolean}
       */
      showAiChat: false
    };
  },
  mounted() {
    // 监听来自header的事件
    window.addEventListener('toggle-ai-chat', this.toggleAiChat);
  },
  beforeUnmount() {
    window.removeEventListener('toggle-ai-chat', this.toggleAiChat);
  },
  methods: {
    /**
     * 切换AI聊天窗口的显示状态
     * 通过取反showAiChat变量来控制窗口的显示与隐藏
     * @returns {void}
     */
    toggleAiChat() {
      this.showAiChat = !this.showAiChat
    },

    /**
     * 处理AI聊天窗口关闭事件
     * 接收子组件传递的值并更新父组件的显示状态
     * @param {boolean} value - 窗口是否显示的状态值
     * @returns {void}
     */
    handleAiChatClose(value: boolean) {
      this.showAiChat = value;
    }
  }
})
</script>

<template>
 <div class="app-container">
  <router-view />
    <!-- AI聊天窗口组件 -->
   <!-- AI聊天侧边栏容器 -->
   <teleport to="body">
     <div v-show="showAiChat" class="ai-sidebar-wrapper">
      <AiChatWindow v-model="showAiChat" @update:modelValue="handleAiChatClose"/>
    </div>
   </teleport>
  </div>
</template>

<style>
/* AI聊天侧边栏基础样式 */
.ai-sidebar-wrapper{
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 10000;
}
.ai-sidebar-wrapper > *{
  pointer-events: auto;
}
/* AI聊天侧边栏展开状态样式 */
.ai-sidebar-open{
  right: 0;
}
.ai-sidebar-overlay{
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(2px);
  z-index: 9999;
}

:deep(.ai-chat-dialog){
  width: 100% !important;
  height: 100%;
  margin: 0 !important;
  max-width: none;
}
:deep(.ai-chat-dialog .el-dialog__body){
  height: calc(100vh - 120px);
}
:deep(.chat-box){
  height: calc(100vh - 120px);
}

/* 全局加载状态样式 */
.loading-global {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.loading-spinner {
  width: 50px;
  height: 50px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #409eff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

/* 全局错误提示样式 */
.error-global {
  position: fixed;
  top: 20px;
  right: 20px;
  padding: 15px 20px;
  background-color: #fef0f0;
  border: 1px solid #fde2e2;
  color: #f56c6c;
  border-radius: 4px;
  z-index: 9999;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
</style>
